<?php

/**
 * @获取当前操作名
 * @CONTROLLER_NAME 控制器
 * @ACTION_NAME     方法
 */
function getaction() {
        $ruleId = M("AuthRule")->where(array('name' => CONTROLLER_NAME . "/" . ACTION_NAME))->getField('pid');
        $parent = M("AuthRule")->where(array('id' => $ruleId))->getField('title');
        return $parent;
}

function get_pleased($status) {
        switch ($status) {
                case 1:
                        return "不满意";
                        break;
                case 2:
                        return "一般";
                        break;
                case 3:
                        return "不错";
                        break;
                case 4:
                        return "满意";
                        break;
                case 5:
                        return "非常满意";
                        break;
        }
}

/**
 * 正则表达式验证网址
 *
 * @param string $str    所要验证的网址
 * @return boolean
 */
function isUrl($str) {
        if (!$str) {
                return false;
        }
        return preg_match('#(http|https|ftp|ftps)://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?#i', $str) ? true : false;
}

//加密
function pwHash($password) {
        return md5(C('AUTH_KEY') . md5(C('AUTH_KEY') . $password));
}

/**
 * 获取客户端IP地址
 * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
 * @param boolean $adv 是否进行高级模式获取（有可能被伪装） 
 * @return mixed
 */
function get_client_ip($type = 0, $adv = true) {
        $type = $type ? 1 : 0;
        static $ip = NULL;
        if ($ip !== NULL)
                return $ip[$type];
        if ($adv) {
                if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                        $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
                        $pos = array_search('unknown', $arr);
                        if (false !== $pos)
                                unset($arr[$pos]);
                        $ip = trim($arr[0]);
                }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
                        $ip = $_SERVER['HTTP_CLIENT_IP'];
                } elseif (isset($_SERVER['REMOTE_ADDR'])) {
                        $ip = $_SERVER['REMOTE_ADDR'];
                }
        } elseif (isset($_SERVER['REMOTE_ADDR'])) {
                $ip = $_SERVER['REMOTE_ADDR'];
        }
        // IP地址合法验证
        $long = sprintf("%u", ip2long($ip));
        $ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
        return $ip[$type];
}

/**
 * 格式化字节大小
 * @param  number $size      字节数
 * @param  string $delimiter 数字和单位分隔符
 * @return string            格式化后的带单位的大小
 */
function format_bytes($size, $delimiter = '') {
        $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
        for ($i = 0; $size >= 1024 && $i < 5; $i++) {
                $size /= 1024;
        }
        return round($size, 2) . $delimiter . $units[$i];
}

/**
 * 验证字符串中是否含有非法字符
 *
 * @param string $string    待验证的字符串
 * @return boolean
 */
function isInvalidStr($string) {
        if (!$string) {
                return false;
        }
        return preg_match('#[!#$%^&*(){}~`"\';:?+=<>/\[\]]+#', $string) ? true : false;
}

/**
 * 用正则表达式验证邮证编码
 *
 * @param integer $num    所要验证的邮政编码
 * @return boolean
 */
function isPostNum($num) {
        if (!$num) {
                return false;
        }
        return preg_match('#^[1-9][0-9]{5}$#', $num) ? true : false;
}

/**
 * 用正则表达式验证手机号码(中国大陆区)
 * @param integer $num    所要验证的手机号
 * @return boolean
 */
function isMobile($num) {
        if (!$num) {
                return false;
        }
        return preg_match('#^13[\d]{9}$|14^[0-9]\d{8}|^15[0-9]\d{8}$|^18[0-9]\d{8}|^17[0-9]\d{8}$#', $num) ? true : false;
}

/**
 * 检查字符串是否为空
 *
 * @access public
 * @param string $string 字符串内容
 * @return boolean
 */
function isMust($string = null) {
        //参数分析
        if (is_null($string)) {
                return false;
        }
        return empty($string) ? false : true;
}

/**
 * 检查字符串长度
 *
 * @access public
 * @param string $string 字符串内容
 * @param integer $min 最小的字符串数
 * @param integer $max 最大的字符串数
 */
function isLength($string = null, $min = 0, $max = 255) {
        //参数分析
        if (is_null($string)) {
                return false;
        }
        //获取字符串长度
        $length = strlen(trim($string));
        return (($length >= (int) $min) && ($length <= (int) $max)) ? true : false;
}

/**
 * 字符串截取
 * @param string $str 被截取字符串
 * @param integer $start 起始位置
 * @param integer $length 截取位置
 * @param string $charset 编码
 * @param boolean $suffix 后缀
 * @return string 截取后字符串
 */
function msubstr($str, $length, $start = 0, $charset = "utf-8", $suffix = true) {
        $str = strip_tags($str);
        if (function_exists("mb_substr"))
                return mb_substr($str, $start, $length, $charset);
        elseif (function_exists('iconv_substr')) {
                return iconv_substr($str, $start, $length, $charset);
        }
        $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
        $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
        $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
        $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
        preg_match_all($re[$charset], $str, $match);
        $slice = join("", array_slice($match[0], $start, $length));
        if ($suffix)
                return $slice . "…";
        return $slice;
}

/**
 * 去除HTML标识
 * @param string $str 字符串
 * @return string
 */
function htmlclear($str) {
        if (empty($str))
                return;
        if ($str == "")
                return $str;
        $str = preg_replace("/\s+/", " ", $str); //过滤多余回车 
        $str = preg_replace("/<[ ]+/si", "<", $str); //过滤<__("<"号后面带空格) 

        $str = preg_replace("/<\!--.*?-->/si", "", $str); //注释 
        $str = preg_replace("/<(\!.*?)>/si", "", $str); //过滤DOCTYPE 
        $str = preg_replace("/<(\/?html.*?)>/si", "", $str); //过滤html标签 
        $str = preg_replace("/<(\/?head.*?)>/si", "", $str); //过滤head标签 
        $str = preg_replace("/<(\/?meta.*?)>/si", "", $str); //过滤meta标签 
        $str = preg_replace("/<(\/?body.*?)>/si", "", $str); //过滤body标签 
        $str = preg_replace("/<(\/?link.*?)>/si", "", $str); //过滤link标签 
        $str = preg_replace("/<(\/?form.*?)>/si", "", $str); //过滤form标签 
        $str = preg_replace("/cookie/si", "COOKIE", $str); //过滤COOKIE标签 

        $str = preg_replace("/<(applet.*?)>(.*?)<(\/applet.*?)>/si", "", $str); //过滤applet标签 
        $str = preg_replace("/<(\/?applet.*?)>/si", "", $str); //过滤applet标签 

        $str = preg_replace("/<(style.*?)>(.*?)<(\/style.*?)>/si", "", $str); //过滤style标签 
        $str = preg_replace("/<(\/?style.*?)>/si", "", $str); //过滤style标签 

        $str = preg_replace("/<(title.*?)>(.*?)<(\/title.*?)>/si", "", $str); //过滤title标签 
        $str = preg_replace("/<(\/?title.*?)>/si", "", $str); //过滤title标签 

        $str = preg_replace("/<(object.*?)>(.*?)<(\/object.*?)>/si", "", $str); //过滤object标签 
        $str = preg_replace("/<(\/?objec.*?)>/si", "", $str); //过滤object标签 

        $str = preg_replace("/<(noframes.*?)>(.*?)<(\/noframes.*?)>/si", "", $str); //过滤noframes标签 
        $str = preg_replace("/<(\/?noframes.*?)>/si", "", $str); //过滤noframes标签 

        $str = preg_replace("/<(i?frame.*?)>(.*?)<(\/i?frame.*?)>/si", "", $str); //过滤frame标签 
        $str = preg_replace("/<(\/?i?frame.*?)>/si", "", $str); //过滤frame标签 

        $str = preg_replace("/<(script.*?)>(.*?)<(\/script.*?)>/si", "", $str); //过滤script标签 
        $str = preg_replace("/<(\/?script.*?)>/si", "", $str); //过滤script标签 
        $str = preg_replace("/javascript/si", "Javascript", $str); //过滤script标签 
        $str = preg_replace("/vbscript/si", "Vbscript", $str); //过滤script标签 
        $str = preg_replace("/on([a-z]+)\s*=/si", "On\\1=", $str); //过滤script标签 
        $str = preg_replace("/&#/si", "&＃", $str); //过滤script标签，如javAsCript:alert( 
        $search = array("'<script[^>]*?>.*?</script>'si", // 去掉 javascript
            "'<[\/\!]*?[^<>]*?>'si", // 去掉 HTML 标记
            "'([\r\n])[\s]+'", // 去掉空白字符
            "'&(quot|#34);'i", // 替换 HTML 实体
            "'&(amp|#38);'i",
            "'&(lt|#60);'i",
            "'&(gt|#62);'i",
            "'&(nbsp|#160);'i",
            "'&(iexcl|#161);'i",
            "'&(cent|#162);'i",
            "'&(pound|#163);'i",
            "'&(copy|#169);'i",
            "'&#(\d+);'");      // 作为 PHP 代码运行

        $replace = array("",
            "",
            "\\1",
            "\"",
            "&",
            "<",
            ">",
            " ",
            chr(161),
            chr(162),
            chr(163),
            chr(169),
            "chr(\\1)");

        $str = preg_replace_callback($search, function($replace) {
                return $replace;
        }, $str);

        $str = str_replace(array(' ', '　'), "", $str);
        return strip_tags($str);
}

function deldir($dir) {
        $dh = opendir($dir);
        while ($file = readdir($dh)) {
                if ($file != "." && $file != "..") {
                        $fullpath = $dir . "/" . $file;
                        if (!is_dir($fullpath)) {
                                unlink($fullpath);
                        } else {
                                deldir($fullpath);
                        }
                }
        }
        closedir($dh);
        if (rmdir($dir)) {
                return true;
        } else {
                return false;
        }
}
